package cn.wangjie.spark.store.es

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{SparkSession}
import org.elasticsearch.spark.rdd.EsSpark

object ReadES {

  def main(args: Array[String]): Unit = {

    val spark = SparkSession
      .builder()
      .appName("Spark Read ES")
      .master("local")
      .config("pushdown", "true")
      .config("es.nodes", "localhost")
      .config("es.port", "9200")
      .getOrCreate()

    val query =
      s"""
         |{
         |    "query":{"match_all":{}},
         |    "_source":["chatMessages"]  //chatMessages是所需查询的字段，貌似没啥用
         |}
       """
        .stripMargin
    //这里的索引类型要与ES中的 【_index,_type】一致，不然会报错
    val data: RDD[(String, collection.Map[String, AnyRef])] = EsSpark.esRDD(spark.sparkContext, "myrow", query)

    //    data.take(1).foreach(println)
    data.take(10).foreach(row => {
      println(row)
    })
  }
}
